package com.itheima.mobilesafe69.activity;

import java.util.List;

import com.itheima.mobilesafe69.R;
import com.itheima.mobilesafe69.db.dao.BlackNumberDao;
import com.itheima.mobilesafe69.db.domain.BlackNumberInfo;
import com.itheima.mobilesafe69.utils.ToastUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

//抽取holder过程,google市场

//1,复用convertView对象,之前有条目对象,就复用,否则就去创建
//2,ViewHolder,减少findViewById次数,将findViewById已经找到的控件,做一个存储,存储到ViewHolder,viewHolder存储到复用的convertView中
//3,将ViewHolder定义成静态,字节码加载一次

//4,分页算法(每一次加载20条数据,下一次加载的数据要添加到上一次数据后面),数据库,网络请求

public class BlackNumberActivity extends Activity {
	private ListView lv_list_blacknumber;
	private Button bt_add;
	private BlackNumberDao mDao;
	private List<BlackNumberInfo> mBlackNumberList;
	private MyAdapter mAdapter;
	private int mode;
	private boolean isload = false;
	private int mTotalCount;
	
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(mAdapter == null){
				mAdapter = new MyAdapter();
				lv_list_blacknumber.setAdapter(mAdapter);
			}else{
				//加载更多的时候,因为已经有数据适配,只需要做刷新即可
				mAdapter.notifyDataSetChanged();
				//false允许开启下一次的加载过程
				isload = false;
			}
			
		};
	};
	
	class MyAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			return mBlackNumberList.size();
		}

		@Override
		public BlackNumberInfo getItem(int position) {
			return mBlackNumberList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(convertView == null){
				//1,创建一个要去展示的view对象,布局xml--->view
				holder = new ViewHolder();
				convertView = View.inflate(getApplicationContext(), R.layout.list_item_black_number, null);
				
				//2,获取一个条目中的控件
				holder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
				holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_mode);
				holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
				
				//给convertView设置一个tag,设置的tag就是要去存储的holder对象
				convertView.setTag(holder);
			}else{
				//将存储在convertView中的holder对象获取出来,给复用的条目去做控件的赋值操作
				holder = (ViewHolder) convertView.getTag();
			}
			
			//3,给控件赋值
			final BlackNumberInfo blackNumberInfo = mBlackNumberList.get(position);
			holder.tv_phone.setText(blackNumberInfo.phone);
			
			holder.iv_delete.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					//1,数据库中删除一条数据
					mDao.delete(blackNumberInfo.phone);
					//2,在集合中,删除一条数据
					mBlackNumberList.remove(blackNumberInfo);
					//3,告知数据适配器刷新
					mAdapter.notifyDataSetChanged();
				}
			});
			
			switch (blackNumberInfo.mode) {
			case 1:
				//短信
				holder.tv_mode.setText("拦截短信");
				break;
			case 2:
				//电话
				holder.tv_mode.setText("拦截电话");
				break;
			case 3:
				//所有
				holder.tv_mode.setText("拦截所有");
				break;
			}
			return convertView;
		}
	}
	
	/**
	 * @author qbc
	 * 临时存储的三个对象
	 */
	static class ViewHolder{
		TextView tv_phone;
		TextView tv_mode;
		ImageView iv_delete;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_black_number);
		
		initUI();
		initData();
	}

	private void initData() {
		//查询数据库获取数据,线程池(java基础)
		new Thread(){
			public void run() {
				//逆序查询20条数据
				mBlackNumberList = mDao.find(0);
				mTotalCount = mDao.findCount();
				
				//告知主线程,listView可以去使用查询到的数据
				mHandler.sendEmptyMessage(0);
			};
		}.start();
	}

	private void initUI() {
		mDao = BlackNumberDao.getInstance(this);
		lv_list_blacknumber = (ListView) findViewById(R.id.lv_list_blacknumber);
		bt_add = (Button) findViewById(R.id.bt_add);
		
		bt_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//弹出对话框,输入拦截黑名单号码
				showDialog();
			}
		});
		
		//对listview滚动做事件监听
		lv_list_blacknumber.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				//滚动状态发生改变方法
				//OnScrollListener.SCROLL_STATE_FLING;正在飞速的滚动
				//OnScrollListener.SCROLL_STATE_TOUCH_SCROLL;用户触摸滚动
				//OnScrollListener.SCROLL_STATE_IDLE;滚动状态有滚动--->空闲
				
				//如果由滚动变成空闲,并且滚动到最后一个条目(最后一个可见条目的索引>=集合大小-1)则需要加载更多数据
				//scrollState当前状态
				if(mBlackNumberList!=null){
					if(scrollState == OnScrollListener.SCROLL_STATE_IDLE && 
							lv_list_blacknumber.getLastVisiblePosition()>=mBlackNumberList.size()-1
							&& !isload){
						
						if(mBlackNumberList.size()<mTotalCount){
							//加载下一页数据
							new Thread(){
								public void run() {
									//下一页查询到的数据,
									List<BlackNumberInfo> moreData = mDao.find(mBlackNumberList.size());
									//添加到上一页数据集合的后面
									mBlackNumberList.addAll(moreData);
									//本次正在加载,不能进行下一次加载,下一次加载必须在收到上一次加载完成的消息以后再去执行
									isload = true;
									//告知主线程,listView可以去使用查询到的数据
									mHandler.sendEmptyMessage(0);
									
								};
							}.start();
						}else{
							ToastUtil.show(getApplicationContext(), "没有更多数据...");
						}
					}
				}
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				//滚动过程中调用方法
			}
		});
	}

	protected void showDialog() {
		Builder builder = new AlertDialog.Builder(this);
		mode = 1;
		//自定义对话框的样式
		final AlertDialog dialog = builder.create();
		View view = View.inflate(getApplicationContext(), R.layout.dialog_add_black_number, null);
		
		final EditText et_phone = (EditText) view.findViewById(R.id.et_phone);
		
		Button bt_submit = (Button) view.findViewById(R.id.bt_submit);
		Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);
		
		RadioGroup rg_group = (RadioGroup) view.findViewById(R.id.rg_group);
		
		//监听选中状态的改变(短信--->电话)
		rg_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_sms:
					mode = 1;
					break;
				case R.id.rb_call:
					mode = 2;
					break;
				case R.id.rb_all:
					mode = 3;
					break;
				}
			}
		});
		
		//1,在点击确认按钮的时候
		bt_submit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//2,获取拦截电话电话号码
				String phone = et_phone.getText().toString().trim();
				//3,获取按钮组中选中的拦截类型
				//4,插入数据库
				mDao.insert(phone,mode);
				//5,创建一个BlacknumberInfo对象,将此对象更新到填充数据适配器的集合中去
				BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
				blackNumberInfo.mode = mode;
				blackNumberInfo.phone = phone;
				
				mBlackNumberList.add(0, blackNumberInfo);
				//6,通知数据适配器刷新,刷新更改后的数据
				mAdapter.notifyDataSetChanged();
				
				dialog.dismiss();
			}
		});
		
		bt_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		
		dialog.setView(view, 0, 0, 0, 0);
		dialog.show();
	}
}
